﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gift.DBUtility;//Please add references
using Gift.Model;
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALPointsForOrder
    /// </summary>
    public partial class DALPointsForOrder
    {
        public DALPointsForOrder()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string orderNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbPointsForOrder");
            strSql.Append(" where orderNo=@orderNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@orderNo", SqlDbType.NVarChar,50)			};
            parameters[0].Value = orderNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbPointsForOrder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbPointsForOrder(");
            strSql.Append("orderNo,orderUser,orderStatus,deliveryStatus,payStatus,consignee,region,address,zipcode,telephone,mobile,postscript,orderIntegral,goodsEntitys,amount,createTime,modifyTime)");
            strSql.Append(" values (");
            strSql.Append("@orderNo,@orderUser,@orderStatus,@deliveryStatus,@payStatus,@consignee,@region,@address,@zipcode,@telephone,@mobile,@postscript,@orderIntegral,@goodsEntitys,@amount,@createTime,@modifyTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@orderNo", SqlDbType.NVarChar,50),
					new SqlParameter("@orderUser", SqlDbType.Int,4),
					new SqlParameter("@orderStatus", SqlDbType.Int,4),
					new SqlParameter("@deliveryStatus", SqlDbType.Int,4),
					new SqlParameter("@payStatus", SqlDbType.Int,4),
					new SqlParameter("@consignee", SqlDbType.NVarChar,20),
					new SqlParameter("@region", SqlDbType.NVarChar,200),
					new SqlParameter("@address", SqlDbType.NVarChar,200),
					new SqlParameter("@zipcode", SqlDbType.NVarChar,20),
					new SqlParameter("@telephone", SqlDbType.NVarChar,50),
					new SqlParameter("@mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@postscript", SqlDbType.NVarChar,200),
					new SqlParameter("@orderIntegral", SqlDbType.Int,4),
					new SqlParameter("@goodsEntitys", SqlDbType.Int,4),
					new SqlParameter("@amount", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@modifyTime", SqlDbType.DateTime)};
            parameters[0].Value = model.orderNo;
            parameters[1].Value = model.orderUser;
            parameters[2].Value = model.orderStatus;
            parameters[3].Value = model.deliveryStatus;
            parameters[4].Value = model.payStatus;
            parameters[5].Value = model.consignee;
            parameters[6].Value = model.region;
            parameters[7].Value = model.address;
            parameters[8].Value = model.zipcode;
            parameters[9].Value = model.telephone;
            parameters[10].Value = model.mobile;
            parameters[11].Value = model.postscript;
            parameters[12].Value = model.orderIntegral;
            parameters[13].Value = model.goodsEntitys;
            parameters[14].Value = model.amount;
            parameters[15].Value = model.createTime;
            parameters[16].Value = model.modifyTime;


            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbPointsForOrder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbPointsForOrder set ");
            strSql.Append("orderUser=@orderUser,");
            strSql.Append("orderStatus=@orderStatus,");
            strSql.Append("deliveryStatus=@deliveryStatus,");
            strSql.Append("payStatus=@payStatus,");
            strSql.Append("consignee=@consignee,");
            strSql.Append("region=@region,");
            strSql.Append("address=@address,");
            strSql.Append("zipcode=@zipcode,");
            strSql.Append("telephone=@telephone,");
            strSql.Append("mobile=@mobile,");
            strSql.Append("postscript=@postscript,");
            strSql.Append("orderIntegral=@orderIntegral,");
            strSql.Append("goodsEntitys=@goodsEntitys,");
            strSql.Append("amount=@amount,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("modifyTime=@modifyTime,");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@orderUser", SqlDbType.Int,4),
					new SqlParameter("@orderStatus", SqlDbType.Int,4),
					new SqlParameter("@deliveryStatus", SqlDbType.Int,4),
					new SqlParameter("@payStatus", SqlDbType.Int,4),
					new SqlParameter("@consignee", SqlDbType.NVarChar,20),
					new SqlParameter("@region", SqlDbType.NVarChar,200),
					new SqlParameter("@address", SqlDbType.NVarChar,200),
					new SqlParameter("@zipcode", SqlDbType.NVarChar,20),
					new SqlParameter("@telephone", SqlDbType.NVarChar,50),
					new SqlParameter("@mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@postscript", SqlDbType.NVarChar,200),
					new SqlParameter("@orderIntegral", SqlDbType.Int,4),
					new SqlParameter("@goodsEntitys", SqlDbType.Int,4),
					new SqlParameter("@amount", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@modifyTime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@orderNo", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.orderUser;
            parameters[1].Value = model.orderStatus;
            parameters[2].Value = model.deliveryStatus;
            parameters[3].Value = model.payStatus;
            parameters[4].Value = model.consignee;
            parameters[5].Value = model.region;
            parameters[6].Value = model.address;
            parameters[7].Value = model.zipcode;
            parameters[8].Value = model.telephone;
            parameters[9].Value = model.mobile;
            parameters[10].Value = model.postscript;
            parameters[11].Value = model.orderIntegral;
            parameters[12].Value = model.goodsEntitys;
            parameters[13].Value = model.amount;
            parameters[14].Value = model.createTime;
            parameters[15].Value = model.modifyTime;
            parameters[16].Value = model.id;
            parameters[17].Value = model.orderNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbPointsForOrder ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string orderNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbPointsForOrder ");
            strSql.Append(" where orderNo=@orderNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@orderNo", SqlDbType.NVarChar,50)			};
            parameters[0].Value = orderNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbPointsForOrder ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbPointsForOrder GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,orderNo,orderUser,orderStatus,deliveryStatus,payStatus,consignee,region,address,zipcode,telephone,mobile,postscript,orderIntegral,goodsEntitys,amount,createTime,modifyTime from tbPointsForOrder ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            Gift.Model.tbPointsForOrder model = new Gift.Model.tbPointsForOrder();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["orderNo"] != null && ds.Tables[0].Rows[0]["orderNo"].ToString() != "")
                {
                    model.orderNo = ds.Tables[0].Rows[0]["orderNo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["orderUser"] != null && ds.Tables[0].Rows[0]["orderUser"].ToString() != "")
                {
                    model.orderUser = int.Parse(ds.Tables[0].Rows[0]["orderUser"].ToString());
                }
                if (ds.Tables[0].Rows[0]["orderStatus"] != null && ds.Tables[0].Rows[0]["orderStatus"].ToString() != "")
                {
                    model.orderStatus = int.Parse(ds.Tables[0].Rows[0]["orderStatus"].ToString());
                }
                if (ds.Tables[0].Rows[0]["deliveryStatus"] != null && ds.Tables[0].Rows[0]["deliveryStatus"].ToString() != "")
                {
                    model.deliveryStatus = int.Parse(ds.Tables[0].Rows[0]["deliveryStatus"].ToString());
                }
                if (ds.Tables[0].Rows[0]["payStatus"] != null && ds.Tables[0].Rows[0]["payStatus"].ToString() != "")
                {
                    model.payStatus = int.Parse(ds.Tables[0].Rows[0]["payStatus"].ToString());
                }
                if (ds.Tables[0].Rows[0]["consignee"] != null && ds.Tables[0].Rows[0]["consignee"].ToString() != "")
                {
                    model.consignee = ds.Tables[0].Rows[0]["consignee"].ToString();
                }
                if (ds.Tables[0].Rows[0]["region"] != null && ds.Tables[0].Rows[0]["region"].ToString() != "")
                {
                    model.region = ds.Tables[0].Rows[0]["region"].ToString();
                }
                if (ds.Tables[0].Rows[0]["address"] != null && ds.Tables[0].Rows[0]["address"].ToString() != "")
                {
                    model.address = ds.Tables[0].Rows[0]["address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["zipcode"] != null && ds.Tables[0].Rows[0]["zipcode"].ToString() != "")
                {
                    model.zipcode = ds.Tables[0].Rows[0]["zipcode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["telephone"] != null && ds.Tables[0].Rows[0]["telephone"].ToString() != "")
                {
                    model.telephone = ds.Tables[0].Rows[0]["telephone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["mobile"] != null && ds.Tables[0].Rows[0]["mobile"].ToString() != "")
                {
                    model.mobile = ds.Tables[0].Rows[0]["mobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["postscript"] != null && ds.Tables[0].Rows[0]["postscript"].ToString() != "")
                {
                    model.postscript = ds.Tables[0].Rows[0]["postscript"].ToString();
                }
                if (ds.Tables[0].Rows[0]["orderIntegral"] != null && ds.Tables[0].Rows[0]["orderIntegral"].ToString() != "")
                {
                    model.orderIntegral = int.Parse(ds.Tables[0].Rows[0]["orderIntegral"].ToString());
                }
                if (ds.Tables[0].Rows[0]["goodsEntitys"] != null && ds.Tables[0].Rows[0]["goodsEntitys"].ToString() != "")
                {
                    model.goodsEntitys = int.Parse(ds.Tables[0].Rows[0]["goodsEntitys"].ToString());
                }
                if (ds.Tables[0].Rows[0]["amount"] != null && ds.Tables[0].Rows[0]["amount"].ToString() != "")
                {
                    model.amount = int.Parse(ds.Tables[0].Rows[0]["amount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["modifyTime"] != null && ds.Tables[0].Rows[0]["modifyTime"].ToString() != "")
                {
                    model.modifyTime = DateTime.Parse(ds.Tables[0].Rows[0]["modifyTime"].ToString());
                }
  
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,orderNo,orderUser,orderStatus,deliveryStatus,payStatus,consignee,region,address,zipcode,telephone,mobile,postscript,orderIntegral,goodsEntitys,amount,createTime,modifyTime  ");
            strSql.Append(" FROM tbPointsForOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,orderNo,orderUser,orderStatus,deliveryStatus,payStatus,consignee,region,address,zipcode,telephone,mobile,postscript,orderIntegral,goodsEntitys,amount,createTime,modifyTime  ");
            strSql.Append(" FROM tbPointsForOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbPointsForOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from tbPointsForOrder T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }



        public bool BulkModifyOrderById(int id, string orderStatus, string deliveryStatus, string payStatus)
        {
            bool flag = false;
            try
            {
                string commandText = string.Format("update tbPointsForOrder set modifyTime=@modifyTime", new object[0]);
                if (orderStatus != "-1")
                {
                    commandText = commandText + string.Format(",orderStatus=@orderStatus", new object[0]);
                }
                if (deliveryStatus != "-1")
                {
                    commandText = commandText + string.Format(",deliveryStatus=@deliveryStatus", new object[0]);
                }
                if (payStatus != "-1")
                {
                    commandText = commandText + string.Format(",payStatus=@payStatus", new object[0]);
                }
                commandText = commandText + string.Format(" where id=@id", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@orderStatus", Convert.ToInt32(orderStatus)), new SqlParameter("@deliveryStatus", Convert.ToInt32(deliveryStatus)), new SqlParameter("@payStatus", Convert.ToInt32(payStatus)), new SqlParameter("@modifyTime", DateTime.Now), new SqlParameter("@id", id) };
                if (DbHelperSQL.ExecuteSql(commandText, commandParameters) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public int GetCountByPointsFor(int pointsFor)
        {
            int num = 0;
            try
            {
                string commandText = string.Format("select count(*) from tbPointsForOrder where payStatus=1 and goodsEntitys=@pointsFor", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@pointsFor", pointsFor) };
                num = DbHelperSQL.ExecuteSql( commandText, commandParameters);
            }
            catch
            {
            }
            return num;
        }


        public DataTable GetOrderByOrderUserAndOrderNo(int orderUser, string orderNo)
        {
            DataTable table = new DataTable();
            try
            {
                string commandText = string.Format("select id,orderNo,orderStatus,deliveryStatus,payStatus,orderIntegral,Convert(nvarchar(16),createTime,121) as createTime,goodsEntitys,amount from tbPointsForOrder where orderUser=@orderUser and orderNo like @orderNo order by createTime desc", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@orderUser", orderUser), new SqlParameter("@orderNo", "%" + orderNo + "%") };
                table = DbHelperSQL.Query(commandText, commandParameters).Tables[0];
            }
            catch
            {
            }
            return table;
        }

        public DataTable GetOrderInfoByOrderNo(string orderNo, int orderUser)
        {
            DataTable table = new DataTable();
            try
            {
                string commandText = string.Format("select id,orderNo,orderUser,orderStatus,deliveryStatus,payStatus,consignee,region,address,zipCode,telephone,mobile,postscript,orderIntegral,Convert(nvarchar(16),createTime,121) as createTime,goodsEntitys,amount from tbPointsForOrder where orderNo=@orderNo and orderUser=@orderUser", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@orderNo", orderNo), new SqlParameter("@orderUser", orderUser) };
                table = DbHelperSQL.Query(commandText, commandParameters).Tables[0];
            }
            catch
            {
            }
            return table;
        }

        public int GetOrderSumIntegralByTerm(string term)
        {
            int num = 0;
            try
            {
                string commandText = string.Format("select sum(orderIntegral*amount) from tbPointsForOrder" + ((term != "") ? (" where " + term) : ""), new object[0]);
                num = DbHelperSQL.ExecuteSql(commandText);
            }
            catch
            {
            }
            return num;
        }

 

        public bool ModifyOrderStatusAndDeliveryStatusByOrderNo(string orderNo, int orderStatus, int deliveryStatus)
        {
            bool flag = false;
            try
            {
                string commandText = string.Format("update tbPointsForOrder set orderStatus=@orderStatus,deliveryStatus=@deliveryStatus,modifyTime=@modifyTime where orderNo=@orderNo", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@orderStatus", orderStatus), new SqlParameter("@deliveryStatus", deliveryStatus), new SqlParameter("@modifyTime", DateTime.Now), new SqlParameter("@orderNo", orderNo) };
                if (DbHelperSQL.ExecuteSql( commandText, commandParameters) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public bool ModifyOrderStatusByOrderNo(string orderNo, int orderStatus)
        {
            bool flag = false;
            try
            {
                string commandText = string.Format("update tbPointsForOrder set orderStatus=@orderStatus,modifyTime=@modifyTime where orderNo=@orderNo", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@orderStatus", orderStatus), new SqlParameter("@modifyTime", DateTime.Now), new SqlParameter("@orderNo", orderNo) };
                if (DbHelperSQL.ExecuteSql(commandText, commandParameters) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        public bool ModifyPayStatusByOrderNo(string orderNo, int payStatus)
        {
            bool flag = false;
            try
            {
                string commandText = string.Format("update tbPointsForOrder set payStatus=@payStatus,modifyTime=@modifyTime where orderNo=@orderNo", new object[0]);
                SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@payStatus", payStatus), new SqlParameter("@modifyTime", DateTime.Now), new SqlParameter("@orderNo", orderNo) };
                if (DbHelperSQL.ExecuteSql(commandText, commandParameters) > 0)
                {
                    flag = true;
                }
            }
            catch
            {
            }
            return flag;
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbPointsForOrder";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

